package com.lineage.william;

import com.lineage.DatabaseFactory;
import com.lineage.server.datatables.NpcTable;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ScrollShopSellList;
import com.lineage.server.serverpackets.S_ShopSellListCn;
import com.lineage.server.serverpackets.ServerBasePacket;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

public class ItemShop {
  private static ArrayList<ArrayList<Object>> aData = new ArrayList<>();
  
  private static boolean NO_GET_DATA = false;
  
  public static final String TOKEN = ",";
  
  public static boolean forNpcQuest(String s, L1PcInstance pc) {
    ArrayList aTempData = null;
    if (!NO_GET_DATA) {
      NO_GET_DATA = true;
      getData();
    } 
    int i = 0;
    while (i < aData.size()) {
      aTempData = (ArrayList)aData.get(i);
      if (((String)aTempData.get(0)).equals(s) && 
        pc.getInventory().checkItem(((Integer)aTempData.get(3)).intValue(), 1L)) {
        int npcId = ((Integer)aTempData.get(1)).intValue();
        pc.set_temp_adena(((Integer)aTempData.get(2)).intValue());
        if (pc.get_temp_adena() == 40308) {
          pc.set_temp_adena(40308);
          pc.sendPackets((ServerBasePacket)new S_ScrollShopSellList(NpcTable.get().getTemplate(npcId)));
        } else {
          pc.set_temp_adena(((Integer)aTempData.get(2)).intValue());
          pc.sendPackets((ServerBasePacket)new S_ShopSellListCn(pc, npcId));
        } 
      } 
      i++;
    } 
    return false;
  }
  
  private static void getData() {
    Connection con = null;
    try {
      con = DatabaseFactory.get().getConnection();
      Statement stat = con.createStatement();
      ResultSet rset = stat.executeQuery("SELECT * FROM w_自订道具商城");
      ArrayList<Object> aReturn = null;
      String sTemp = null;
      if (rset != null)
        while (rset.next()) {
          aReturn = new ArrayList();
          sTemp = rset.getString("action");
          aReturn.add(0, sTemp);
          aReturn.add(1, Integer.valueOf(rset.getInt("npc编号")));
          aReturn.add(2, Integer.valueOf(rset.getInt("使用货币")));
          aReturn.add(3, Integer.valueOf(rset.getInt("itemid")));
          aData.add(aReturn);
        }  
      if (con != null && !con.isClosed())
        con.close(); 
    } catch (Exception exception) {}
  }
}
